home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c,alt.bbs.proboard
- Subject: Re: Needed: Centering Procedure that ignores escape sequences
- Date: 13 Feb 1996 17:14:47 -0500
- Organization: Montclair State University
- Message-ID: <harmon.824246552@pegasus.montclair.edu>
- References: <4fog4q$24v@news.mountain.net>
- NNTP-Posting-Host: pegasus.montclair.edu
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
- matt.jenkins@westvirginia.com (Light Chaser) writes:
-
- >I spent a whole day trying to come up with a simple and small
- >centering routine. I came up with one, but then ran into the problem
- >that I have to ignore escape sequences.
-
- >center("\n\2This \3text \5contains \1escape \6sequences\t\n");
-
- I take it a permissible solution would be to, within center() on a local
- copy of the string argument, remove the escape sequences. It appears all
- escape sequences are 2-bytes. A slight refinement will be necessary to this
- code if "\\" is to be allowed to remain as "\", but that should be straight-
- forward.
-
- : #define MAXLEN 80
- : #define ESCAPE '\\'
- :
- : int center(const char *line)
- : {
- : char s[MAXLEN]; /* 80 or any other max display line length will do */
- : int leng; /* length of string s */
- : int i, j; /* position within string and general indicies */
- :
- : strcpy( s, line); /* s <- copy of line, line will not be modified */
- : i = 0;
- : leng = strlen( s );
- :
- : while( i < leng ) /* when i == leng, s is stripped of escapes */
- : if ( s[i] == ESCAPE ) { /* this can be any leading escape char */
- : for( j = i; (j + 2) <= leng; j++ )
- : s[j] = s[(j + 2)]; /* overwrite by shifting forward */
- : leng -= 2; /* length is now 2 shorter */
- : }
- : else i++; /* when length not 2 shorter, process 1 position further */
- : s[(leng + 1)] = '\0'; /* housekeeping, nul was never advanced */
- :
- : i = MAXLEN / 2 - leng / 2; /* number of leading spaces to print */
- : for( j = 0; j < i; j++ ) fputchar(' '); /* stdout is assumed */
- : for( j = 0; j < leng; j++ ) fputchar(s[j]); /* more eff. than printf() */
- : fputchar('\n'); /* print newline for good measure */
- :
- : return( leng + i + 1 ); /* return number of chars printed */
- : } /* just as easy to return stripped str leng */
-
- You can invoke it as center("\\3Hello\\3!"); or strcpy(svar, "\\3Hello!");
- and then center(svar); but it is significant in C that when you create any
- strings including the backslash as an escape character (since it is also an
- escape character in the C language) that you repeat it twice. '\\' is a
- backslash, '\3' is a character with ASCII value 3. If you can't do that in
- your case, another function to change backslashes to asterisks or double
- them before hand may be necessary. Note that strlen("\\ABC\\"); is 5, not
- 7.
- -- Stone
- --
- # Derek Harmon (aka Stonelight) harmon@pegasus.montclair.edu
- # - Computer Science Undergrad, Montclair State University, NJ
- # - My views are my own, nobody else is this creative. 3;)>
- ... C programmer run, C programmer crash, Ada programmer.
-
-